package com.pn.warehouse.service.impl;

import com.pn.warehouse.dto.AssignRoleDto;
import com.pn.warehouse.entity.Result;
import com.pn.warehouse.entity.Role;
import com.pn.warehouse.entity.User;
import com.pn.warehouse.entity.UserRole;
import com.pn.warehouse.mapper.RoleMapper;
import com.pn.warehouse.mapper.UserMapper;
import com.pn.warehouse.mapper.UserRoleMapper;
import com.pn.warehouse.page.Page;
import com.pn.warehouse.service.UserService;
import com.pn.warehouse.utils.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    //根据账号查询用户的信息
    @Override
    public User fingUserByCode(String userCode) {
        return userMapper.fingUserByCode(userCode);
    }

    //分页查询用户的信息
    @Override
    public Page queryByUserPage(Page page, User user) {
        //1、查询用户总行数
        Integer count = userMapper.findRowCount(user);
        //2、分页查询用户信息
        List<User> users = userMapper.findUserByPage(page, user);

        //3、封装到page中
        page.setTotalNum(count);
        page.setResultList(users);

        return page;
    }

    //添加用户
    @Override
    public int addUser(User user) {
        int i = userMapper.addUser(user);
        return i;
    }

    //启用或者禁用用户的状态
    @Override
    public int setUserState(User user) {
        int i = userMapper.setUserState(user);
        return i;
    }

    //给用户分配角色
    @Transactional //开启事务
    @Override
    public void assignRole(AssignRoleDto assignRoleDto) {
        //1、根据用户id在用户角色关系表中删除之前分配的角色
        userRoleMapper.removeUserRoleByUid(assignRoleDto.getUserId());

        for (String name : assignRoleDto.getRoleCheckList()) {
            //2、根据角色名字查询到角色的id
            Integer roleId = roleMapper.findRoleIdByName(name);

            //3、把新分配的角色添加到用户角色关系表中
            UserRole userRole = new UserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(assignRoleDto.getUserId());
            userRoleMapper.insertUserRole(userRole);
        }
    }

    //根据用户的ids逻辑删除用户
    @Override
    public Result deleteUserById(List<Integer> userIdList) {
        int i = userMapper.deleteUserById(userIdList);
        if (i > 0){
            return Result.ok("删除用户成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除用户失败！");
    }

    //根据用户id修改用户昵称
    @Override
    public Result updateUserNameById(User user) {
        int i = userMapper.updateUserNameById(user);
        if (i > 0){
            return Result.ok("修改用户成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"修改用户失败！");
    }

    //根据用户id修改密码
    @Override
    public Result updatePwdById(Integer userId) {
        String password = DigestUtil.hmacSign("123456");
        int i = userMapper.updatePwdById(userId, password);
        if (i > 0){
            return Result.ok("重置密码成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"重置密码失败！");
    }

}
